package cn.doitedu.json;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class JsonParseDemo {

    public static void main(String[] args) {

        String json = "{\n" +
                "  \"order_id\": \"order001\",\n" +
                "  \"order_amount\": 1200.0,\n" +
                "  \"member_id\": 100,\n" +
                "  \"address\": \"上海市闵行区联航路1888号上计信息楼A座\",\n" +
                "  \"items\": [\n" +
                "    {\n" +
                "      \"item_id\": \"item001\",\n" +
                "      \"item_price\": 18.8,\n" +
                "      \"quantity\": 10,\n" +
                "      \"style\": {\n" +
                "        \"size\": \"XXL\",\n" +
                "        \"color\": \"RED\"\n" +
                "      }\n" +
                "    },\n" +
                "    {\n" +
                "      \"item_id\": \"item003\",\n" +
                "      \"item_price\": 38.2,\n" +
                "      \"quantity\": 20,\n" +
                "      \"style\": {\n" +
                "        \"size\": \"XL\",\n" +
                "        \"color\": \"GREEN\"\n" +
                "      }\n" +
                "    }\n" +
                "  ]\n" +
                "}";

        JSONObject jsonObject = JSON.parseObject(json);

        String orderId = jsonObject.getString("order_id");
        System.out.println(orderId);

        double orderAmount = jsonObject.getDoubleValue("order_amount");
        System.out.println(orderAmount);

        JSONArray array = jsonObject.getJSONArray("items");
        for(int i=0 ;i<array.size();i++) {

            JSONObject itemObject = array.getJSONObject(i);


            String itemId = itemObject.getString("item_id");
            System.out.println(itemId);

            JSONObject styleObject = itemObject.getJSONObject("style");
            String color = styleObject.getString("color");
            System.out.println(color);

        }
    }


}
